<?php
class Handler_db
{
	//链接一个PEAR数据库，返回一个PEAR DB对象
	public static function create_peardb_handle($connect_str = DB_CONNECT_STR) {
	    require_once(PEAR_PATH."DB.php");
	    global $static_dbh;
	    if (null != $static_dbh)
	    {
	        return $static_dbh;
	    }
	    $static_dbh = DB::connect($connect_str);
	    $static_dbh->query("set names utf8");
	    $static_dbh->setFetchMode(DB_FETCHMODE_ASSOC);
	    return $static_dbh;
	}
	
	//关闭数据库链接
	public static function close_peardb_handle() {
	    require_once(PEAR_PATH."DB.php");
	    global $static_dbh;
	    $static_dbh->disconnect();
	}
	//
	//数据库相关函数,参数包含数据库连接信息:host, user, password, dbname,返回:句柄或null
	//
	public static function db_connect()
	{
		
		$db_link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, true);
		if (!$db_link)
		{
			return null;
		}
		if (!mysql_select_db(DB_NAME, $db_link))
		{
			return null;
		}
		return $db_link;
	}
	
	//只读
	//链接一个PEAR数据库，返回一个PEAR DB对象
	public static function create_readonly_peardb_handle($connect_str = DB_CONNECT_STR_READONLY) {
	    require_once(PEAR_PATH."DB.php");
	    global $static_dbh_ro;
	    if (null != $static_dbh)
	    {
	        return $static_dbh_ro;
	    }
	    $static_dbh_ro = DB::connect($connect_str);
	    $static_dbh_ro->query("set names utf8");
	    $static_dbh_ro->setFetchMode(DB_FETCHMODE_ASSOC);
	    return $static_dbh_ro;
	}
	
	//关闭数据库链接
	public static function close_readonly_peardb_handle() {
	    require_once(PEAR_PATH."DB.php");
	    global $static_dbh_ro;
	    $static_dbh_ro->disconnect();
	}
	//
	//数据库相关函数,参数包含数据库连接信息:host, user, password, dbname,返回:句柄或null
	//
	public static function db_readonly_connect()
	{
		
		$db_link = mysql_connect(DB_HOST_RO, DB_USER_RO, DB_PASSWORD_RO, true);
		if (!$db_link)
		{
			return null;
		}
		if (!mysql_select_db(DB_NAME_RO, $db_link))
		{
			return null;
		}
		return $db_link;
	}
}
?>
